/*  LocationModel.java - Class for a location and its signature 
 
        Copyright (C) 2009 Devendra Laulkar

        This program is free software: you can redistribute it and/or modify
        it under the terms of the GNU General Public License as published by
        the Free Software Foundation version 2 of the License. 

        This program is distributed in the hope that it will be useful,
        but WITHOUT ANY WARRANTY; without even the implied warranty of
        MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
        GNU General Public License for more details.

        You should have received a copy of the GNU General Public License
        along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/
package com.intellBatt;

import java.util.Date;
import java.util.HashMap;

public class LocationModel {

	public HashMap<String, WifiNetwork> m = new HashMap<String, WifiNetwork>();
	private String locationName;
	private long id;
	private Date lastSeen;
	private int count;
	
	
	public void incrementCount()
	{
		count++;
	}
	public LocationModel()
	{
		locationName = null;
		id = -1;
		setLastSeen(new Date());
		setCount(0);
	}
	
	public LocationModel(String name)
	{
		this();
		locationName = name;
	}
	
	public String getLocationName() {
		return locationName;
	}

	public void setLocationName(String locationName) {
		this.locationName = locationName;
	}

	public HashMap<String, WifiNetwork> getMap()
	{
		return m;
	}
	
	public void addNetwork(String bssid)
	{
		m.put(bssid, new WifiNetwork(bssid));
	}
	
	public void addNetwork(WifiNetwork nw)
	{
		m.put(nw.getBssid(), nw);
	}
	public void addSignalReading(String bssid, int val)
	{
		WifiNetwork nw = m.get(bssid);
		
		if(nw == null)
		{
			nw = new WifiNetwork(bssid);
			m.put(bssid, nw);
		}
		
		nw.addSignal(val);
	}
	
	
	public double compareNetwork(String bssid, int val)
	{
		WifiNetwork nw = m.get(bssid);
		
		if(nw == null)
		{
			return 0.0;
		}
		
		if(val > nw.getAverageVal() - 2 && val < nw.getAverageVal() + 2)
		{
			//Log.i("Match", "Perfect match" + nw.getBssid());
			return 1.0;
		}
		
		if(val < nw.getMaxValue() && val > nw.getMinValue())
		{
			//Log.i("Match", "Between Max and min" + nw.getBssid());
			return 0.6;
		}
		else
		{
			//Log.i("Match", "NW present" + nw.getBssid());
			//Log.i("Match", "Max is :" + nw.getMaxValue() + " Min is " + nw.getMinValue() + " Avg is " + nw.getAverageVal());
			return 0.1;
		}
	}
	
	public double match(LocationModel model)
	{
		
		return this.match(model.m);
	}
	
	public double match(HashMap<String, WifiNetwork> map)
	{
		double sum = 0.0;
		int common = 0;
		
		for(String bssid: map.keySet() )
		{
			WifiNetwork nw = map.get(bssid);
			
			double sim = compareNetwork(bssid, nw.getLastSignalVal()); 
			if(sim != 0.0)
			{
				common += 1;
			}
			sum += sim;
		}
		
		return sum / (m.size() + map.size() - common);
		
	}
	
	public double matchMap(HashMap<String,Integer> m1)
	{
		double sum = 0.0;
		int common = 0;
		
		for(String bssid: m1.keySet() )
		{
			double sim = compareNetwork(bssid, m1.get(bssid)); 
			if(sim != 0.0)
			{
				common += 1;
			}
			sum += sim;
		}
		
		return sum / (m.size() + m1.size() - common);
	}

	public void setId(long id) {
		this.id = id;
	}

	public long getId() {
		return id;
	}

	public void setLastSeen(Date lastSeen) {
		this.lastSeen = lastSeen;
	}

	public Date getLastSeen() {
		return lastSeen;
	}

	public void setCount(int count) {
		this.count = count;
	}

	public int getCount() {
		return count;
	}
}
